<template>
  <div v-if="total>0" class="pagination-container">
    <el-pagination
      ref="pagination"
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 0
    },
    limit: {
      type: Number,
      default: 10
    },
    pageSizes: {
      type: Array,
      default() {
        return [5, 10, 50, 100]
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val // 强制设置pageSize
      this.$emit('update:page', 1)
      this.$emit('pagination', { page: 1, pageSize: val })
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, pageSize: this.pageSize })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .pagination-container {
    text-align: right;
  }
  .el-pagination__total,
  .el-pagination__sizes {
    float: left;
  }
  .pagination-container.hidden {
    display: none;
  }
</style>
